package com.cet.designapplication.pay.strategy;

import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.cet.designapplication.pojo.Order;
import com.cet.designapplication.utils.Constants;

/**
 * @author NSOC
 */
public class AlipayStrategy implements PayStrategyInterface{
    @Override
    public String pay(Order order) {
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(
                Constants.ALIPAY_GATEWAY,
                Constants.APP_ID,
                Constants.APP_PRIVATE_KEY,
                "JSON",
                "UTF-8",
                Constants.ALIPAY_PUBLIC_KEY,
                Constants.SIGN_TYPE
        );
        AlipayTradePagePayRequest pagePayRequest = new AlipayTradePagePayRequest();
        pagePayRequest.setReturnUrl(Constants.CALLBACK_URL);
        pagePayRequest.setBizContent("{" +
                "\"out_trade_no\": \"" + order.getOrderId() + "\"," +
                "\"total_amount\": \"" + order.getPrice() + "\"," +
                "\"subject\": \"" + "陈恩涛" + "\"," +
                "\"body\": " + "\"商品描述\"," +
                "\"product_code\": " + "\"FAST_INSTANT_TRADE_PAY\"" +
                "}");
        try {
            return alipayClient.pageExecute(pagePayRequest, "GET").getBody();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
}
